package cn.lkx.serviceucenter.controller;


import cn.lkx.commonutils.JwtUtils;
import cn.lkx.commonutils.R;
import cn.lkx.commonutils.orderVo.OrderMemberVo;
import cn.lkx.serviceucenter.entity.UcenterMember;
import cn.lkx.serviceucenter.entity.vo.RegisterVo;
import cn.lkx.serviceucenter.service.UcenterMemberService;
import io.swagger.annotations.Api;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 会员表 前端控制器  会员的登录注册等
 * </p>
 *
 * @author lkx
 * @since 2022-05-09
 */
@Api(description = "前台用户中心")
@RestController
@RequestMapping("/serviceucenter/member")
@CrossOrigin
public class UcenterMemberController {
    @Autowired
    UcenterMemberService memberService;

    //会员登录方法
    @PostMapping("login")
    public R login(@RequestBody UcenterMember member){
        String token = memberService.login(member); //返回token值 （单点登录）
        return R.ok().data("token",token);
    }
    //会员注册
    @PostMapping("register")
    public R register(@RequestBody RegisterVo registerVo){
        boolean result = memberService.register(registerVo);
        if (result){
            return R.ok();
        }else return R.error();

    }
    //根据token获取用户信息
    @GetMapping("getLoginInfo")
    public R getLoginInfo(HttpServletRequest request){
        //根据request对象获取头信息，返回会员id
        String menberId = JwtUtils.getMemberIdByJwtToken(request);
        UcenterMember member = memberService.getById(menberId);
        return R.ok().data("memberInfo",member);
    }

    // 给order模块远程调用 根据会员id查询到会员信息(需要 会员昵称，会员手机邮箱)
    @PostMapping("getMemberToOrder/{id}")
    public OrderMemberVo getMemberToOrder(@PathVariable String id) {
        //根据用户id获取用户信息
        UcenterMember ucenterMember = memberService.getById(id);
        OrderMemberVo orderMemberVo = new OrderMemberVo();
        BeanUtils.copyProperties(ucenterMember,orderMemberVo);
        return orderMemberVo;
    }

    //获取某一天的注册人数, 给statistics 模块统计调用
    @GetMapping("countRegisterByDay/{day}")
    public R countRegisterByDay(@PathVariable String day){
        Integer count = memberService.countRegisterByDay(day);
        return R.ok().data("countRegister",count);
    }


}

